Telegram Group & Telegram Channel
👣 Что выйдет код ?

go 
package main

import "fmt"

// Пара обобщённого типа
type Pair[T any, U any] struct {
First T
Second U
}

// Метод, меняющий местами элементы
func (p Pair[T, U]) Swap() Pair[U, T] {
return Pair[U, T]{First: p.Second, Second: p.First}
}

// Карта обобщённого типа
type Mapping[K comparable, V any] map[K]V

func main() {
// явно указываем T=int, U=string
p1 := Pair[int, string]{First: 1, Second: "one"}

// начиная с Go 1.21 можно вывести параметры типа из литерала!
// здесь компилятор сам понимает, что T=string, U=int
p2 := Pair{"two", 2}

// аналогично для обобщённого alias-типа map
m := Mapping{"beta": 2}

// откатываем пару p2
swapped := p2.Swap()

fmt.Printf("%T %v\n", p2, p2)
fmt.Printf("%T %v\n", swapped, swapped)
fmt.Printf("%T %v\n", m, m)
}




Ответ: Код на Go 1.24 выведет следующее:

`main.Pair string int {two 2}`
`main.Pair int string {2 two}`
`main.Mapping string int map["beta":2]`

**Объяснение:**

1. **`p1 := Pairint, string{First: 1, Second: "one"}`** и **`p2 := Pair{"two", 2}`**:
* Создаются две переменные типа `Pair`.
* Для `p1` типы `T` (int) и `U` (string) указываются явно.
* Для `p2`, начиная с Go 1.21, компилятор автоматически выводит типы `T` (string) и `U` (int) из значений литерала `{"two", 2}`.

2. **`m := Mapping{"beta": 2}`**:
* Создается переменная `m` типа `Mapping`. `Mapping` является псевдонимом для `mapstringint`. Компилятор выводит типы ключа (string) и значения (int) из литерала `{"beta": 2}`.

3. **`swapped := p2.Swap()`**:
* Вызывается метод `Swap()` для `p2`. Метод `Swap()` возвращает новую структуру `Pair`, в которой типы `U` и `T` поменяны местами, а также значения полей `First` и `Second`. Таким образом, `swapped` будет иметь тип `Pairint, string` и значение `{2, "two"}`.

4. **`fmt.Printf("%T %v\n", p2, p2)`**:
* `%T` выводит тип переменной `p2`, который был выведен как `main.Pair string int`.
* `%v` выводит значение переменной `p2`, которое равно `{two 2}`.

5. **`fmt.Printf("%T %v\n", swapped, swapped)`**:
* `%T` выводит тип переменной `swapped`, который является результатом метода `Swap()` и равен `main.Pair int string`.
* `%v` выводит значение переменной `swapped`, которое равно `{2 two}`.

6. **`fmt.Printf("%T %v\n", m, m)`**:
* `%T` выводит тип переменной `m`, который является `main.Mapping string int`, что эквивалентно `mapstringint`.
* `%v` выводит значение переменной `m`, которое является картой `map["beta":2]`.

Таким образом, программа выведет тип и значение каждой из созданных и модифицированных переменных.
Please open Telegram to view this post
VIEW IN TELEGRAM



tg-me.com/golangtests/766
Create:
Last Update:

👣 Что выйдет код ?

go 
package main

import "fmt"

// Пара обобщённого типа
type Pair[T any, U any] struct {
First T
Second U
}

// Метод, меняющий местами элементы
func (p Pair[T, U]) Swap() Pair[U, T] {
return Pair[U, T]{First: p.Second, Second: p.First}
}

// Карта обобщённого типа
type Mapping[K comparable, V any] map[K]V

func main() {
// явно указываем T=int, U=string
p1 := Pair[int, string]{First: 1, Second: "one"}

// начиная с Go 1.21 можно вывести параметры типа из литерала!
// здесь компилятор сам понимает, что T=string, U=int
p2 := Pair{"two", 2}

// аналогично для обобщённого alias-типа map
m := Mapping{"beta": 2}

// откатываем пару p2
swapped := p2.Swap()

fmt.Printf("%T %v\n", p2, p2)
fmt.Printf("%T %v\n", swapped, swapped)
fmt.Printf("%T %v\n", m, m)
}




Ответ: Код на Go 1.24 выведет следующее:

`main.Pair string int {two 2}`
`main.Pair int string {2 two}`
`main.Mapping string int map["beta":2]`

**Объяснение:**

1. **`p1 := Pairint, string{First: 1, Second: "one"}`** и **`p2 := Pair{"two", 2}`**:
* Создаются две переменные типа `Pair`.
* Для `p1` типы `T` (int) и `U` (string) указываются явно.
* Для `p2`, начиная с Go 1.21, компилятор автоматически выводит типы `T` (string) и `U` (int) из значений литерала `{"two", 2}`.

2. **`m := Mapping{"beta": 2}`**:
* Создается переменная `m` типа `Mapping`. `Mapping` является псевдонимом для `mapstringint`. Компилятор выводит типы ключа (string) и значения (int) из литерала `{"beta": 2}`.

3. **`swapped := p2.Swap()`**:
* Вызывается метод `Swap()` для `p2`. Метод `Swap()` возвращает новую структуру `Pair`, в которой типы `U` и `T` поменяны местами, а также значения полей `First` и `Second`. Таким образом, `swapped` будет иметь тип `Pairint, string` и значение `{2, "two"}`.

4. **`fmt.Printf("%T %v\n", p2, p2)`**:
* `%T` выводит тип переменной `p2`, который был выведен как `main.Pair string int`.
* `%v` выводит значение переменной `p2`, которое равно `{two 2}`.

5. **`fmt.Printf("%T %v\n", swapped, swapped)`**:
* `%T` выводит тип переменной `swapped`, который является результатом метода `Swap()` и равен `main.Pair int string`.
* `%v` выводит значение переменной `swapped`, которое равно `{2 two}`.

6. **`fmt.Printf("%T %v\n", m, m)`**:
* `%T` выводит тип переменной `m`, который является `main.Mapping string int`, что эквивалентно `mapstringint`.
* `%v` выводит значение переменной `m`, которое является картой `map["beta":2]`.

Таким образом, программа выведет тип и значение каждой из созданных и модифицированных переменных.

BY Go tests


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/golangtests/766

View MORE
Open in Telegram


Go tests Telegram | DID YOU KNOW?

Date: |

What Is Bitcoin?

Bitcoin is a decentralized digital currency that you can buy, sell and exchange directly, without an intermediary like a bank. Bitcoin’s creator, Satoshi Nakamoto, originally described the need for “an electronic payment system based on cryptographic proof instead of trust.” Each and every Bitcoin transaction that’s ever been made exists on a public ledger accessible to everyone, making transactions hard to reverse and difficult to fake. That’s by design: Core to their decentralized nature, Bitcoins aren’t backed by the government or any issuing institution, and there’s nothing to guarantee their value besides the proof baked in the heart of the system. “The reason why it’s worth money is simply because we, as people, decided it has value—same as gold,” says Anton Mozgovoy, co-founder & CEO of digital financial service company Holyheld.

Export WhatsApp stickers to Telegram on Android

From the Files app, scroll down to Internal storage, and tap on WhatsApp. Once you’re there, go to Media and then WhatsApp Stickers. Don’t be surprised if you find a large number of files in that folder—it holds your personal collection of stickers and every one you’ve ever received. Even the bad ones.Tap the three dots in the top right corner of your screen to Select all. If you want to trim the fat and grab only the best of the best, this is the perfect time to do so: choose the ones you want to export by long-pressing one file to activate selection mode, and then tapping on the rest. Once you’re done, hit the Share button (that “less than”-like symbol at the top of your screen). If you have a big collection—more than 500 stickers, for example—it’s possible that nothing will happen when you tap the Share button. Be patient—your phone’s just struggling with a heavy load.On the menu that pops from the bottom of the screen, choose Telegram, and then select the chat named Saved messages. This is a chat only you can see, and it will serve as your sticker bank. Unlike WhatsApp, Telegram doesn’t store your favorite stickers in a quick-access reservoir right beside the typing field, but you’ll be able to snatch them out of your Saved messages chat and forward them to any of your Telegram contacts. This also means you won’t have a quick way to save incoming stickers like you did on WhatsApp, so you’ll have to forward them from one chat to the other.

Go tests from ua


Telegram Go tests
FROM USA